package pkg

import (
	"bytes"
	"context"
	"encoding/json"
	"github.com/spf13/viper"
	"io"
	"log"
	"net/http"
	"wail/app/model"
)

// 创建一个实例化对象
type User struct {
	ctx    context.Context
	client http.Client
	viper  *viper.Viper
}

// NewApp creates a new App application struct
func NewApp() *User {
	//读取配置文件
	v := viper.New()
	//设置配置文件路径
	v.SetConfigFile("app/config/config.yaml")
	v.ReadInConfig()
	//返回配置
	return &User{
		client: http.Client{},
		viper:  v,
	}
}

// 发送邮箱验证码
func (u *User) SendCode(email string) model.SendCodeResponse {
	//设置请求参数
	data := model.SendCodeRequest{Email: email}

	//将请求参数json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}
	//构造请求函数
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/SendCode", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create new request:" + err.Error(),
		}
	}

	//发送请求到服务端
	result, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应内容
	all, err := io.ReadAll(result.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	log.Println(string(all))
	return model.SendCodeResponse{
		Code:    200,
		Message: "Success to send code",
	}
}

// 用户实现注册
func (u *User) Register(email string, pass string, code string) model.SendCodeResponse {
	//实例化注册的数据
	data := model.RegisterRequest{
		Email: email,
		Pass:  pass,
		Code:  code,
	}

	//数据json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建http请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/Register", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}

	//发送请求
	result, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应内容
	all, err := io.ReadAll(result.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	log.Println(string(all))
	return model.SendCodeResponse{
		Code:    200,
		Message: "Success to send code",
	}
}

// 实现账号密码登陆
func (u *User) LoginPass(email string, pass string) model.LoginPassResponse {
	//实例化登陆账号密码
	data := model.LoginPassRequest{
		Email: email,
		Pass:  pass,
	}

	//数据json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建http请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/LoginPass", bytes.NewReader(marshal))
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}

	//发送请求
	result, err := u.client.Do(request)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	body, err := io.ReadAll(result.Body)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to read response:" + err.Error(),
		}
	}

	log.Println(string(body))

	var returnResult model.LoginPassResponse
	err = json.Unmarshal(body, &returnResult)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to unmarshal return body:" + err.Error(),
		}
	}

	return returnResult
}

// 实现账号验证码登陆
func (u *User) LoginCode(email string, code string) model.LoginPassResponse {
	//获取登陆参数
	data := model.LoginCode{
		Email: email,
		Code:  code,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/LoginCode", bytes.NewReader(marshal))
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.LoginPassResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.LoginPassResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 获取首页
func (u *User) GetSupplierIndex(email string, token string) model.GetSupplierIndexResponse {
	//获取登陆参数
	data := model.LoginCode{
		Email: email,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.GetSupplierIndexResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/GetSupplierIndex", bytes.NewReader(marshal))
	if err != nil {
		return model.GetSupplierIndexResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.GetSupplierIndexResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.GetSupplierIndexResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.GetSupplierIndexResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.GetSupplierIndexResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 搜索商品
func (u *User) SearchGoods(email string, KeysWords string, token string) model.SupplierSearchGoodsResponse {
	//获取登陆参数
	data := model.SupplierSearchGoods{
		Email:    email,
		KeyWords: KeysWords,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/SupplierSearchGoods", bytes.NewReader(marshal))
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SupplierSearchGoodsResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 获取所有商品信息
func (u *User) GetSupplierGoods(email string, token string) model.SupplierSearchGoodsResponse {
	//获取登陆参数
	data := model.SupplierSearchGoods{
		Email: email,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/GetSupplierGoods", bytes.NewReader(marshal))
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SupplierSearchGoodsResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SupplierSearchGoodsResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 获取单个商品详情
func (u *User) GetSupplierGoodsDetail(email string, token string, id string) model.GetSupplierGoodsDetailResponse {
	//获取登陆参数
	data := model.GetSupplierGoodsDetailRequest{
		Email: email,
		Id:    id,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.GetSupplierGoodsDetailResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/GetSupplierGoodsDetail", bytes.NewReader(marshal))
	if err != nil {
		return model.GetSupplierGoodsDetailResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.GetSupplierGoodsDetailResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.GetSupplierGoodsDetailResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.GetSupplierGoodsDetailResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.GetSupplierGoodsDetailResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 修改商品状况
func (u *User) UpdateSupplierGoods(email string, token string, id string, status int) model.SendCodeResponse {
	//获取登陆参数
	data := model.UpdateSupplierGoodsRequest{
		Email:  email,
		Id:     id,
		Status: status,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/ModifySupplierGoodsStatus", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SendCodeResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 添加商品
func (u *User) AddSupplierGoods(supplier string, name string, pic string, desc string, price float64, stock int, unit string, token string) model.SendCodeResponse {
	data := model.AddSupplierGoodsRequest{
		Supplier: supplier,
		Name:     name,
		Pic:      pic,
		Desc:     desc,
		Price:    price,
		Stock:    stock,
		Unit:     unit,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/AddSupplierGoods", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SendCodeResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 删除商品
func (u *User) DeleteSupplierGoods(email string, id string, token string) model.SendCodeResponse {
	//获取登陆参数
	data := model.DeleteSupplierGoodsRequest{
		Email:   email,
		GoodsId: id,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/DeleteSupplierGoods", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SendCodeResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 搜索订单
func (u *User) SearchSupplierOrder(email string, id string, token string) model.SearchSupplierOrderResponse {
	//获取登陆参数
	data := model.SearchSupplierOrderRequest{
		Email: email,
		Id:    id,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/SearchSupplierOrder", bytes.NewReader(marshal))
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SearchSupplierOrderResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 获取订单首页数据
func (u *User) GetSupplierOrderData(email string, token string) model.GetSupplierOrderDataResponse {
	//获取登陆参数
	data := model.SearchSupplierOrderRequest{
		Email: email,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.GetSupplierOrderDataResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/GetSupplierOrderData", bytes.NewReader(marshal))
	if err != nil {
		return model.GetSupplierOrderDataResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.GetSupplierOrderDataResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.GetSupplierOrderDataResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.GetSupplierOrderDataResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.GetSupplierOrderDataResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 查看订单详情
func (u *User) GetSupplierOrderDetail(email string, id string, token string) model.SearchSupplierOrderResponse {
	//获取登陆参数
	data := model.SearchSupplierOrderRequest{
		Email: email,
		Id:    id,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/GetSupplierOrderDetail", bytes.NewReader(marshal))
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SearchSupplierOrderResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SearchSupplierOrderResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 实现商品修改
func (u *User) ModifySupplierGoods(email string, token string, name string, pic string, desc string, unit string, price string, stock int) model.SendCodeResponse {
	//获取登陆参数
	data := model.ModifySupplierGoodsRequest{
		Email: email,
		Name:  name,
		Pic:   pic,
		Desc:  desc,
		Price: price,
		Stock: stock,
		Unit:  unit,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/ModifySupplierGoods", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SendCodeResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 实现订单发货
func (u *User) SupplierDeliverOrder(email string, orderId string, deliver string, deliverId string, token string) model.SendCodeResponse {
	//获取登陆参数
	data := model.SupplierDeliverOrderRequest{
		Email:     email,
		OrderId:   orderId,
		Deliver:   deliver,
		DeliverId: deliverId,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/SupplierDeliverOrder", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SendCodeResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}

// 商家切换头像
func (u *User) SupplierChangeHead(email string, pic string, token string) model.SendCodeResponse {
	//获取登陆参数
	data := model.SupplierChangeHeadRequest{
		Email: email,
		Pic:   pic,
	}

	//将data json化
	marshal, err := json.Marshal(data)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to marshal data:" + err.Error(),
		}
	}

	//创建请求
	request, err := http.NewRequest("POST", u.viper.GetString("baseUrl")+"/supplier/SupplierChangeHead", bytes.NewReader(marshal))
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to create request:" + err.Error(),
		}
	}
	request.Header.Set("Authorization", "Bearer "+token)

	//发送请求
	do, err := u.client.Do(request)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to send request:" + err.Error(),
		}
	}

	//读取响应的内容
	body, err := io.ReadAll(do.Body)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	//进行json bind
	var result model.SendCodeResponse
	err = json.Unmarshal(body, &result)
	if err != nil {
		return model.SendCodeResponse{
			Code:    500,
			Message: "Failed to read response body:" + err.Error(),
		}
	}

	return result
}
